import {Component, OnInit, ViewEncapsulation, Output, OnDestroy} from '@angular/core';
import {Router, ActivatedRoute} from "@angular/router";
import {ToolsService} from "@shared/service/tools/tools.service";
import {Subject} from "rxjs";

import {OrderService} from "@order/order.service";
import {OrderDetailRespVO, SNSResult} from "@interface/index";



@Component({
  selector: 'seed-order-detail',
  templateUrl: 'order-detail.component.html',
  styleUrls: ['order-detail.component.scss'],
  encapsulation: ViewEncapsulation.None
})

export class OrderDetailComponent implements OnInit, OnDestroy {
  private orderId: string;
  public goodsList: any = [];
  public goodsOne: any = {};
  public show = false;
  public orderInfo: OrderDetailRespVO = new OrderDetailRespVO();
  public ngUnsubscribe: Subject<void> = new Subject<void>();
  public times = {
    timer: null, // 定时器
    payTime: '', // 剩余支付时间戳
    nowTime: '',
    day: '0',
    hour: '00',
    minute: '00',
    second: '00',
  };

  constructor(public tools: ToolsService,
              public route: ActivatedRoute,
              public router: Router,
              public orderService: OrderService) {
    this.route.params.subscribe(params => {
      const orderId = params['orderId'];
      this.getDetailInfo(orderId);
    });
  }

  getDetailInfo(orderId: string) {
    this.orderService.getOrdersDetail(orderId)
      .takeUntil(this.ngUnsubscribe)
      .map(res => this.disposeOrderStatus(res))
      .subscribe(res => {
        this.goodsList = res.data.details;
        this.orderInfo = res.data;
        this.goodsOne = res.data.details[0];
        this.times.timer = setInterval(this.handleCountdown.bind(this), 1000);
      });
  }

  /**
   * @description 处理订单状态 如果订单状态等于3,5,9,实付款(total)就等于0
   * @anthor @chenwh576
   * @date 20171024
   * @param data
   * @returns {GoodsOrderRespVO}
   */
  disposeOrderStatus(res: SNSResult<OrderDetailRespVO>) {
    if (res.data.status === 3 || res.data.status === 5 || res.data.status === 9 || res.data.status === 10) {
      res.data.total = 0;
    }
    return res;
  }

  /**
   * @description 在订单详情中点击按钮，跳转页面
   * @anthor @zhupw1360
   * @date 20171106
   */
  // 跳转支付页面
  goToPayment() {
    this.router.navigate(['/payment'], {queryParams: {orderNumber: this.orderInfo.orderNumber}});
  }

  // 跳转报名页面
  goToSignUp() {
    const params = {
      goodsCode: this.orderInfo.details[0].goodsCode,
      goodsCateCode1: this.goodsOne.goodsCateCode1,
      goodsCateCode: this.goodsOne.goodsCateCode,
      skuCode: this.goodsOne.skuCode,
      orderNumber: this.orderInfo.orderNumber
    };
    this.orderService.getGoodsTemplateUrl(params);
  }


  // 处理活动倒计时
  handleCountdown() {
    let t = this.orderInfo.timeExpireSecond -= 1 ;
    this.times.payTime = t.toString();
    if ( this.orderInfo.status !== 1 || t <= 0 ) { // 订单状态 非 待付款 清除定时器，
      t = 0;
      clearInterval(this.times.timer);
    }

    let hour, minute, second;
    this.times.day = Math.floor(t / 60 / 60 / 24).toString();
    hour = Math.floor(t / 60 / 60 % 24).toString();
    minute = Math.floor(t / 60 % 60).toString();
    second = Math.floor(t % 60).toString();
    this.times.hour = this.handleTime(hour);
    this.times.minute = this.handleTime(minute);
    this.times.second = this.handleTime(second);
  }
  // 处理个位数时间
  handleTime(time) {
    if (time > 0 && time < 10) {
      return `0${time}`;
    } else {
      return time;
    }
  }


  /*
  *@description 查看取票人信息
  * @author zhoukf1349
  * @date 2018-04-13
  */
  goToPayInfo() {
    this.router.navigate(['/goods/p/ticket-info'], {queryParams: {orderNumber: this.orderInfo.orderNumber}});
  }
  /*
  *@description 查看物流
  * @author zhoukf1349
  * @date 2018-04-13
  */
  checkLogistics() {
    this.router.navigate(['/goods/p/check-logistics'], {queryParams: {orderNumber: this.orderInfo.orderNumber}});
  }

  ngOnInit() {

  }

  ngOnDestroy() {
    this.ngUnsubscribe.next();
    this.ngUnsubscribe.complete();
  }
}
